package com.fer.controller;

import com.fer.model.Goods;
import com.fer.service.GoodsService;
import com.fer.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author why
 * @date 2021/5/28
 **/

@Controller
@RequestMapping("/goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;


    //查询所有商品的信息
    @GetMapping("/{shop_id}")
    @ResponseBody
    public Map<String,Object>  getAllGoods(@PathVariable("shop_id") Integer shop_id){
        Map<String,Object> result = new HashMap<>();
        List<Goods> goodsList = goodsService.selectAllGoods(shop_id);
        System.out.println(goodsList);
        if(goodsList.size()==0){
            result.put("code",400);
            result.put("message","查找失败");
            return result;
        }
        result.put("data",goodsList);
        result.put("code",200);
        result.put("message","查找成功");
        return result;
    }

    @GetMapping("/{shop_id}/{goods_name}")
    @ResponseBody
    //通过name查询单个商品信息
    public Map<String,Object>  getGoods(@PathVariable("shop_id") Integer shop_id,@PathVariable("goods_name") String goods_name){
        Map<String,Object> result = new HashMap<>();
        List<Goods> goodsList = goodsService.selectGoodByName(shop_id,goods_name);
       // System.out.println(goodsList);
        if(goodsList==null){
            result.put("code",400);
            result.put("message","查找失败");
            return result;
        }
        result.put("code",200);
        result.put("message","查找成功");
        result.put("data",goodsList);
        return result;
    }


    //增加一个商品
    @PostMapping
    @ResponseBody
    public Map<String,Object>  addGood(Goods goods,boolean is_sale){
        goods.set_sale(is_sale);
        Map<String,Object> result = new HashMap<>();
        try{
            goodsService.insertGood(goods);
        }catch (Exception e){
            result.put("code",400);
            result.put("message","添加失败");
            return result;
        }
        result.put("code",200);
        result.put("message","添加成功");
        return result;
    }

    //根据id删商品
    @DeleteMapping("/{goods_id}")
    @ResponseBody
    public Map<String,Object>  deleteGood(@PathVariable("goods_id") Integer goods_id){
        Map<String,Object> result = new HashMap<>();
        try{
            goodsService.deleteGood(goods_id);
        }catch (Exception e){
            result.put("code",400);
            result.put("message","删除失败");
            return result;
        }
        result.put("code",200);
        result.put("message","删除成功");
        return result;
    }


    //修改商品信息
    @PutMapping
    @ResponseBody
    public Map<String,Object>  updateGood(Goods goods,boolean is_sale){
        goods.set_sale((is_sale));
        Map<String,Object> result = new HashMap<>();
        boolean state = goodsService.updateGood(goods);
        if (!state){
            result.put("code",400);
            result.put("message","修改失败");
            return result;
        }
        result.put("code",200);
        result.put("message","修改成功");
        return result;
    }

}
